package com.cash.app.ui.main.fg.home.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import coil3.load
import coil3.request.placeholder
import com.cash.app.R
import com.cash.app.constant.AppConstant.CAN_LOAN
import com.cash.app.constant.AppConstant.OST_GET_CREDIT
import com.cash.app.constant.AppConstant.OST_GET_CREDIT1
import com.cash.app.constant.AppConstant.OS_EMPTY
import com.cash.app.constant.AppConstant.OS_OVERDUE
import com.cash.app.constant.AppConstant.OS_PENDING_REPAY
import com.cash.app.constant.AppConstant.getOrderStatusText
import com.cash.app.databinding.FgHomeLargeItemBinding
import com.cash.app.databinding.FgHomeSmallItemBinding
import com.cash.app.ui.main.fg.home.model.LoanProduct
import com.cash.app.utils.NumberUtils

class HomeListAdapter : ListAdapter<LoanProduct, RecyclerView.ViewHolder>(DIFF_CALLBACK) {

    companion object {
        const val VIEW_TYPE_LARGE = 1
        const val VIEW_TYPE_SMALL = 2

        private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<LoanProduct>() {
            override fun areItemsTheSame(oldItem: LoanProduct, newItem: LoanProduct): Boolean {
                return oldItem.p_id == newItem.p_id
            }

            override fun areContentsTheSame(oldItem: LoanProduct, newItem: LoanProduct): Boolean {
                return oldItem == newItem
            }
        }
    }

    private var currentViewType = VIEW_TYPE_SMALL
    private var canLoan = CAN_LOAN
    var onItemApplyClick: ((product: LoanProduct) -> Unit)? = null

    @SuppressLint("NotifyDataSetChanged")
    fun setViewType(viewType: Int) {
        currentViewType = viewType
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setCanLoan(isCan: Int) {
        canLoan = isCan
        notifyDataSetChanged()
    }

    override fun getItemViewType(position: Int): Int = currentViewType

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            VIEW_TYPE_LARGE -> LargeViewHolder(
                FgHomeLargeItemBinding.inflate(
                    LayoutInflater.from(parent.context), parent, false
                )
            )

            else -> SmallViewHolder(
                FgHomeSmallItemBinding.inflate(
                    LayoutInflater.from(parent.context), parent, false
                )
            )
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val product = getItem(position)
        when (holder) {
            is LargeViewHolder -> holder.bind(product, canLoan)
            is SmallViewHolder -> holder.bind(product, canLoan)
        }
    }

    inner class LargeViewHolder(
        private val binding: FgHomeLargeItemBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(product: LoanProduct, canLoan: Int) {
            binding.apply {
                val ivIcon = binding.root.findViewById<ImageView>(R.id.ivIcon)
                val tvAppName = binding.root.findViewById<TextView>(R.id.tvAppName)
                if (product.p_icon.isNotEmpty()) {
                    ivIcon.load(R.mipmap.app_icon) { // product.p_icon
                        placeholder(R.mipmap.app_icon)
                    }
                }
                tvAppName.text = "Crash demo product"//product.p_name
                if (product.o_id == OS_EMPTY && canLoan == CAN_LOAN) {
                    btnApply.text = OST_GET_CREDIT
                    btnApply.setBackgroundResource(R.drawable.common_btn_bg_gray)
                    btnApply.setTextColor(binding.root.context.resources.getColor(R.color.yellow_1))
                    btnApply.setOnClickListener {
                        onItemApplyClick?.invoke(product)
                    }
                } else {
                    btnApply.text = getOrderStatusText(product.orderStatus)
                    btnApply.setTextColor(binding.root.context.resources.getColor(R.color.black_1))
                    btnApply.setBackgroundResource(R.drawable.common_btn_bg_yellow)
                }
                if (product.orderStatus == OS_PENDING_REPAY || product.orderStatus == OS_OVERDUE) {
                    tvAmount.text = NumberUtils.toRupeeWithComma(product.currentMoney)
                } else {
                    tvAmount.text = NumberUtils.toRupeeWithComma(product.p_money)
                }
            }
        }
    }

    inner class SmallViewHolder(
        private val binding: FgHomeSmallItemBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        @SuppressLint("SetTextI18n")
        fun bind(product: LoanProduct, canLoan: Int) {
            binding.apply {
                if (product.p_icon.isNotEmpty()) {
                    productIcon.load(product.p_icon) {
                        placeholder(R.mipmap.app_icon)
                    }
                }
                tvProductName.text = product.p_name
                tvTenure.text = binding.root.context.getString(
                    R.string.unit_tenure, product.p_peroid.toString()
                )
                tvRate.text = binding.root.context.getString(R.string.unit_rate, product.p_rate)

                if (product.o_id == OS_EMPTY && canLoan == CAN_LOAN) {
                    btnApply.text = OST_GET_CREDIT1
                    btnApply.setBackgroundResource(R.drawable.common_btn_bg_gray)
                    btnApply.setTextColor(binding.root.context.resources.getColor(R.color.yellow_1))
                    btnApply.setOnClickListener {
                        onItemApplyClick?.invoke(product)
                    }
                } else {
                    btnApply.text = getOrderStatusText(product.orderStatus)
                    btnApply.setTextColor(binding.root.context.resources.getColor(R.color.black_1))
                    btnApply.setBackgroundResource(R.drawable.common_btn_bg_yellow)
                }

                if (product.o_id == OS_PENDING_REPAY || product.o_id == OS_OVERDUE) {
                    tvAmount.text = NumberUtils.toRupeeWithComma(product.currentMoney)
                } else {
                    tvAmount.text = NumberUtils.toRupeeWithComma(product.p_money)
                }
            }
        }
    }
} 